﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.SqlClient;
using System.Data;
using System.Data.Common;

using Data_Objects;
using Business_Objects;
namespace Data_Objects.Implementation
{
    //Ko public -> tất cả gọi qua dataacess
    class SqlServerBienLaiTraDao : IBienLaiTraDao
    {
        public SqlServerBienLaiTraDao()
        {
            SQLUtility.TableName = "BienLaiTra";
        }

        //Trả về đối tượng báo ăn
        private BienLaiTra MakeBienLaiTra(DataRow row)
        {
            Guid _id = Guid.Parse(row["ID"].ToString());
            Guid StudentID = Guid.Parse(row["StudentID"].ToString());
            Guid SchoolYearID = Guid.Parse(row["SchoolYearID"].ToString());
            Guid ClassID = Guid.Parse(row["ClassID"].ToString());
            string So = row["So"].ToString();

            DateTime Ngay = DateTime.Parse(row["Ngay"].ToString());
            int TraThang = int.Parse(row["TraThang"].ToString());
            decimal Tongtien = decimal.Parse(row["Tongtien"].ToString());
            int SchoolID = int.Parse(row["SchoolID"].ToString());

            return new BienLaiTra(_id, StudentID, SchoolYearID, ClassID, So, Ngay, TraThang, Tongtien, SchoolID);
        }

        //Lấy danh sách khách hàng trả về datatable
        private IList<BienLaiTra> MakeBienLaiTras(DataTable dt)  
        {
            IList<BienLaiTra> list = new List<BienLaiTra>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(MakeBienLaiTra(row));
            }
            return list;
        }
        public IList<Business_Objects.BienLaiTra> GetBienLaiTra()
        {
            SQLUtility.TableName = "BienLaiTra";
            DbParameter[] param = null;
            DataTable dt = CommandProvider.GetDatatable(SQLUtility.ProceduresGetAll, param);
            return MakeBienLaiTras(dt); 
        }

        public Business_Objects.BienLaiTra GetBienLaiTraByID(string BienLaiTraID)
        {
            SQLUtility.TableName = "BienLaiTra";
            DbParameter[] parameters = new DbParameter[1];
            parameters[0] = new SqlParameter("@ID", BienLaiTraID);
            DataRow row = CommandProvider.GetDataRow(SQLUtility.ProceduresGetByID, parameters);
            if (row == null) { return null; }
            else
            {
                return MakeBienLaiTra(row);
            }
        }

        public void InsertBienLaiTra(Business_Objects.BienLaiTra BienLaiTra)
        {
            SQLUtility.TableName = "BienLaiTra";
            int newID = CommandProvider.Insert(SQLUtility.ProceduresInsert, Take(BienLaiTra), true);
        }

        public int UpdateBienLaiTra(Business_Objects.BienLaiTra BienLaiTra)
        {
            SQLUtility.TableName = "BienLaiTra";
            try { return CommandProvider.Update(SQLUtility.ProceduresUpdate, Take(BienLaiTra)); }
            catch { return -1; }
        }

        public int DeleteBienLaiTra(Business_Objects.BienLaiTra BienLaiTra)
        {
            SQLUtility.TableName = "BienLaiTra";
            try { return CommandProvider.Delete(SQLUtility.ProceduresDelete, Take(BienLaiTra)); }
            catch { return -1; }
        }

        private DbParameter[] Take(BienLaiTra BienLaiTra)
        {
            DbParameter[] parameters = new DbParameter[9];
            parameters[0] = new SqlParameter("@ID", BienLaiTra.Id);
            parameters[1] = new SqlParameter("@StudentID", BienLaiTra.StudentID);
            parameters[2] = new SqlParameter("@SchoolYearID", BienLaiTra.SchoolYearID);
            parameters[3] = new SqlParameter("@ClassID", BienLaiTra.ClassID);
            parameters[4] = new SqlParameter("@So", BienLaiTra.So);
            parameters[5] = new SqlParameter("@Ngay", BienLaiTra.Ngay);

            parameters[6] = new SqlParameter("@TraThang", BienLaiTra.TraThang );
            parameters[7] = new SqlParameter("@Tongtien", BienLaiTra.Tongtien);
            parameters[8] = new SqlParameter("@SchoolID", BienLaiTra.SchoolID);
            return parameters;
        }


        public List<BienLaiTra> GetBienLaiTraByClassIdAndSchoolSchoolYear(string schoolYearID, int schoolId, string classId)
        {
            DbParameter[] parameters = new DbParameter[3];
            parameters[0] = new SqlParameter("@SchoolID", schoolId);
            parameters[1] = new SqlParameter("@SchoolYearID", schoolYearID);
            parameters[2] = new SqlParameter("@ClassID", classId);
            DataTable dt = CommandProvider.GetDatatable("usp_SelectBienLaiTrasByAndSchoolIDAndSchoolYearIDInClass", parameters);
            return MakeBienLaiTras(dt).ToList();
        }


        public int InsertGetId(BienLaiTra obj)
        {
            SQLUtility.TableName = "BienLaiTra";
            int newID = CommandProvider.Insert(SQLUtility.ProceduresInsert, Take(obj), true);
            return newID;
        }
    }
}
